package com.neutech.web;

import com.alibaba.fastjson.JSON;
import com.neutech.dao.SaleDAO;
import com.neutech.dao.impl.SaleDAOImpl;
import com.neutech.entity.Sale;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(urlPatterns = {"/sale"})
public class SaleServlet extends HttpServlet {
    /*@Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    }*/

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        req.setCharacterEncoding("utf-8");
        SaleDAO dao = new SaleDAOImpl();
        List<Sale> sales = dao.findAll();

        // 将Java对象转化成为json格式
        String strSales = JSON.toJSONString(sales);
        // 向前台输出
        resp.getWriter().print(strSales);
        /*System.out.println(sales);*/
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req,resp);
    }
}
